import { save, pages, LoadFrom, Delete } from '@/api/banner/banner'

import { pages as candidateSkillPage } from '@/api/candidateSkill/candidateSkill'

// import { DepartmentTree } from '@/api/systems/systemDepartment'

const state = {
 controllerName: 'banner',
 dataList: {
  listLoading: false,
  list: [],
  departmentTree: [],
  defaultProps: {
   children: 'departmentList',
   key: 'id',
   label: 'name'
  },
  searchForm: {
   LoginName: '',
   pageSize: 10,
   pageIndex: 1,
   dataCount: 0,
   pageCount: 0
  }
 },
 form: {
  formState: false,
  formDepartmentState: false,
  model: {},
  ResetPwdFormState: false,
  ResetPwdModel: {},
  AddressOptions: [],
  candidateSkillList: []
 }
}
const mutations = {
 pageSizeChange (state, par) {
  state.dataList.searchForm.pageSize = par
  this.dispatch(`banner/${state.controllerName}/findList`)
 },
 currentPageChange (state, par) {
  state.dataList.searchForm.pageIndex = par
  this.dispatch(`banner/${state.controllerName}/findList`)
 }
}

const actions = {
 loadNewForm (context) {
  var _state = context.state
  _state.form.model = {}
  _state.form.formState = true
 },
 // 加载表单
 loadForm (context, par) {
  var _state = context.state
  console.log('加载表单参数', par)
  LoadFrom({ ID: par })
   .then(response => {
    const { Data } = response
    console.log(Data)
    _state.form.model = Data

    context.dispatch('getCandidateSkillPage', { id: _state.form.model.FkId })
    _state.form.formState = true
   })
   .catch(error => {
    console.log(error)
   })
 },
 save (context, par) {
  var _state = context.state
  par.sequenceNumber = parseInt(par.sequenceNumber)
  save(par)
   .then(response => {
    const { Data } = response
    console.log(Data)
    global.tools.msg('保存成功!', '成功')
    _state.form.formState = false

    context.dispatch('findList')
   })
   .catch(error => {
    console.log(error)
   })
 },
 findList (context, par) {
  var _state = context.state
  _state.dataList.listLoading = true
  pages(_state.dataList.searchForm)
   .then(res => {
    const { Data } = res
    console.log(Data)
    _state.dataList.list = Data.List
    _state.dataList.searchForm.pageIndex = Data.PageIndex
    _state.dataList.searchForm.pageSize = Data.PageSize
    _state.dataList.searchForm.dataCount = Data.Count
    _state.dataList.searchForm.pageCount = Data.Pages
    _state.dataList.listLoading = false
   })
   .catch(error => {
    console.log(error)
   })
 },
 delete (context, par) {
  console.log(par)
  global.tools.confirm(
   '确定要删除吗？',
   function () {
    Delete({
     id: par
    })
     .then(res => {
      const { Data } = res
      console.log(Data)
      global.tools.msg('删除成功!', '成功')
      context.dispatch('findList')
     })
     .catch(e => {
      console.log(e)
     })
   },
   null,
   '提示'
  )
 },
 getCandidateSkillPage (context, par) {
  console.log('查询列表数据', context, par)
  var _state = context.state
  // "000000000-000-0000-0000-000000000000"
  par.id = !par.id ? '0' : par.id
  par.name = !par.name ? '' : par.name

  candidateSkillPage({
   id: par.id,
   name: par.name,
   pageSize: 200,
   pageIndex: 1
  })
   .then(res => {
    const { Data } = res
    _state.form.candidateSkillList = Data.List
   })
   .catch(e => {
    console.log(e)
   })
 },

 handleNodeClick (context, node) {
  var _state = context.state
  _state.form.model.departmentId = node.id
  _state.form.model.departmentName = node.name
  _state.form.formDepartmentState = false
 },
 handleSuccess (context, res) {
  console.log(res)
  if (res.Status === 1) {
   var _state = context.state
   _state.form.model.ImageUrl = res.Data.Url
   console.log('_state.form.model.CompanyLogo', _state.form.model.CompanyLogo)
  } else {
   global.tools.msg('上传失败，请重新上传！', '提示')
  }
 }
}

export default {
 namespaced: true,
 state,
 mutations,
 actions
}
